package cn.tedu.v1;

import cn.tedu.v1.exception.ServiceException;
import cn.tedu.v1.mapper.UserMapper;
import cn.tedu.v1.model.Permission;
import cn.tedu.v1.model.User;
import cn.tedu.v1.service.IUserService;
import cn.tedu.v1.vo.RegisterVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


import java.util.List;

@SpringBootTest
public class secyrity {
    @Test
    void encode() {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        String pwd = encoder.encode("123");
        System.out.println(pwd);
    }

    @Test
    void match() {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean b = encoder.matches("4444",
                "$2a$10$B7fNKY4jJoM8rhaOBZQp0ul9zpO1Bsr8ahSNjyZEzIK0TAU0VhZvm");
        System.out.println(b);
    }

    @Autowired
    UserMapper userMapper;

    @Test
    void testUser() {
        User user = userMapper.findUserByUsername("pt3");
        System.out.println(user);
        System.out.println(user.getId());
        List<Permission> ps = userMapper.findUserPermissionsById(user.getId());
        for (Permission p : ps) {
            System.out.println(p);
        }
    }

    @Test
    void query() {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("email", "854655@qq.com");
        User user = userMapper.selectOne(query);
        System.out.println(user);
    }

    @Autowired
    IUserService userService;

    @Test
    void addUser() {
        RegisterVo registerVo = new RegisterVo();
        registerVo.setUsername("xide")
                .setEmail("xidesos@163.com")
                .setPassword("666666");
        try {
            userService.registerUser(registerVo);
        } catch (ServiceException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("完成测试");
    }

}
